<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Message extends CCMIS_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('model_studentmessage');
        $this->load->model('model_studentmember');
        $this->load->model('model_studentmessagesendlist');
        $this->load->model('model_studentmessagesent');
        $this->load->model('model_studentmessageinbox');
        $this->load->model('model_studentmessageoutbox');



    }

    public function index()
    {
        $username = $this->session->userdata('username');
        if(!$username)
            $this->login();
        else
            $this->view();
    }

    public function view()
    {
        $id = $this->session->userdata('user_id');

        $inboxmessages = $this->model_studentmessageinbox->getAllByOwnerId($id);
        $sentmessages = $this->model_studentmessagesent->getAllByOwnerId($id);

        $messagesinbox = array();
        $messagessent = array();

        foreach($inboxmessages as $inboxmessage)
        {
            $id = $inboxmessage['StudentMessageID'];
            $inboxid = $inboxmessage[''];
            $message = $this->model_studentmessage->getById($id);

            array_push($messagesinbox, $message);
        }

        foreach($sentmessages as $sentmessage)
        {
            $id = $sentmessage['StudentMessageID'];
            $message = $this->model_studentmessage->getById($id);

            array_push($messagessent, $message);
        }



        $data = array(
            'inboxmessages' => $messagesinbox,
            'sentmessages' => $messagessent,

        );

        $this->setup_view('smis/message/view', $data);
    }


    public function send()
    {
        $receivers = $this->input->post('receive');
        $title = $this->input->post('title');
        $message = $this->input->post('message');
        $sender = $this->session->userdata('user_id');

        $fail = false;

        $receivers = explode(';', $receivers);
        $ids = array();

        foreach($receivers as $receiver)
        {
            $receiver = trim($receiver);

            if($receiver == "")
                continue;

            $id = $this->model_studentmember->getIdByUsernameOrReturnFalse($receiver);
            if($id == false)
            {
                echo 'username: '.$receiver,' not found ';
                $fail = true;
                continue;
            }

            array_push($ids, $id);
        }
        if($fail)
            return;

        $messageid = $this->model_studentmessage->add($sender, $message);


        foreach($ids as $id)
        {
            $this->model_studentmessagesendlist->add($messageid, $id);
            $this->model_studentmessageinbox->add($id, $messageid);
        }

        $this->model_studentmessagesent->add($sender, $messageid);

        //*/

        echo 'title: '.$title.' content: '.$message.' by: '.$sender.' sent to: '.print_r($receivers);
    }


}
